package com.jy.rhin.application.registration;

import cn.hutool.core.util.StrUtil;
import cn.org.chiss.rhin._2015.registration.terminology.RetrieveValueSetRequest;
import cn.org.chiss.rhin._2015.registration.terminology.RetrieveValueSetResponse;
import cn.org.chiss.rhin._2015.registration.terminology.ValueSetQueryRequest;
import cn.org.chiss.rhin._2015.registration.terminology.ValueSetQueryResponse;
import com.jy.api.misc.MiscAPI;
import com.jy.api.misc.dto.WSSYBMXTDaiMaDto;
import com.jy.api.misc.dto.ZhiJiCXDto;
import com.jy.api.misc.dto.ZhiYuDto;
import com.jy.framework.base.utils.Serializer;
import com.jy.rhin.application.assembler.ValueSetAssembler;
import com.jy.rhin.config.RhinFaultSubCode;
import com.jy.rhin.facade.ws.basic.wsEntry.exception.RhinWSException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mayuxuan
 */
@Service
@Slf4j
public class ValueSetService {

    @Resource
    MiscAPI miscApi;

    public ValueSetQueryResponse jiZhiCx(ValueSetQueryRequest message) {
        ZhiJiCXDto zhiJiCXDto = ValueSetAssembler.toDto(message);
        List<ZhiYuDto> list = miscApi.jiZhiCX(zhiJiCXDto).getData();
        if (CollectionUtils.isEmpty(list)) {
            throw new RhinWSException(RhinFaultSubCode.ValueSetNotFoundFault);
        }
        log.debug("/wssy/TRS/jiZhiCX:{}", Serializer.serialize(list));
        ValueSetQueryResponse valueSetQueryResponse = new ValueSetQueryResponse();
        valueSetQueryResponse.setValueSet(list.stream().map(ValueSetAssembler::toValueSet).collect(Collectors.toList()));
        return valueSetQueryResponse;
    }

    public RetrieveValueSetResponse jiZhiHq(RetrieveValueSetRequest message) {
        String biaoShiFu = message.getId().getValue();
        WSSYBMXTDaiMaDto data = miscApi.jiZhiHQ(biaoShiFu).getData();
        log.debug("/wssy/TRS/jiZhiHQ:{}", Serializer.serialize(data));
        if (data == null || StrUtil.isEmpty(data.getBiaoShiFu())) {
            throw new RhinWSException(RhinFaultSubCode.ConceptMapNotFoundFault);
        }
        RetrieveValueSetResponse retrieveValueSetResponse = new RetrieveValueSetResponse();
        retrieveValueSetResponse.setValueSet(ValueSetAssembler.toValueSet(data));
        return retrieveValueSetResponse;
    }

}
